﻿param (
	[Parameter(Mandatory=$true)]
    [ValidatePattern("\d\.\d\.\d\.\d")]
    [string]
	$ReleaseVersionNumber
)

$ErrorActionPreference = "STOP";

$PSScriptFilePath = (Get-Item $MyInvocation.MyCommand.Path).FullName

$SolutionRoot = Split-Path -Path $PSScriptFilePath -Parent

$Is64BitSystem = (Get-WmiObject -Class Win32_OperatingSystem).OsArchitecture -eq "64-bit"
$Is64BitProcess = [IntPtr]::Size -eq 8

# Find the CLR version
$RegistryArchitecturePath = ""
if ($Is64BitProcess) { $RegistryArchitecturePath = "\Wow6432Node" }
$ClrVersion = (Get-ItemProperty -path "HKLM:\SOFTWARE$RegistryArchitecturePath\Microsoft\VisualStudio\10.0")."CLR Version"

# Find MSBuild
$FrameworkArchitecturePath = ""
if ($Is64BitSystem) { $FrameworkArchitecturePath = "64" }
$MSBuild = "$Env:SYSTEMROOT\Microsoft.NET\Framework$FrameworkArchitecturePath\$ClrVersion\MSBuild.exe"

# Make sure we don't have a release folder for this version already
$ReleaseFolder = Join-Path -Path $SolutionRoot -ChildPath "Releases\v$ReleaseVersionNumber$ReleaseSuffix";
if ((Get-Item $ReleaseFolder -ErrorAction SilentlyContinue) -ne $null)
{
	Write-Warning "$ReleaseFolder already exists on your local machine. It will now be deleted."
	Remove-Item $ReleaseFolder -Recurse
}

# Set the version number in SolutionInfo.cs
$SolutionInfoPath = Join-Path -Path $SolutionRoot -ChildPath "SolutionInfo.cs"
(gc -Path $SolutionInfoPath) `
	-replace "(?<=Version\(`")[.\d]*(?=`"\))", $ReleaseVersionNumber |
	sc -Path $SolutionInfoPath -Encoding UTF8

# Build the solution in release mode
$SolutionPath = Join-Path -Path $SolutionRoot -ChildPath "WebAssert.sln"
& $MSBuild "$SolutionPath" /p:Configuration=Release /maxcpucount /t:Clean
if (-not $?)
{
	throw "The MSBuild process returned an error code."
}
& $MSBuild "$SolutionPath" /p:Configuration=Release /maxcpucount
if (-not $?)
{
	throw "The MSBuild process returned an error code."
}

# Run the unit tests?

# Create a temp folder for the release assets
$LibraryReleaseFolder = Join-Path -Path $ReleaseFolder -ChildPath "Library";
New-Item $LibraryReleaseFolder -Type directory

# Copy core library assets to temp folder (dll + pdb + xml)
$CoreBinFolder = Join-Path -Path $SolutionRoot -ChildPath "WebAssert.Core\bin\Release"
Copy-Item "$CoreBinFolder\*.*" -Destination $LibraryReleaseFolder -Include "WebAssertTestLibrary.Core.dll","WebAssertTestLibrary.Core.pdb"

# Copy MS Test library assets to temp folder (dll + pdb + xml)
$MSTestBinFolder = Join-Path -Path $SolutionRoot -ChildPath "WebAssert.MSTest\bin\Release"
Copy-Item "$MSTestBinFolder\*.*" -Destination $LibraryReleaseFolder -Include "WebAssertTestLibrary.MSTest.dll","WebAssertTestLibrary.MSTest.pdb"

# Load ZIP library
$LibraryReleaseZip = Join-Path -Path $ReleaseFolder -ChildPath "WebAssert-v$ReleaseVersionNumber.zip";
Add-Type -Path (Join-Path -Path $SolutionRoot -ChildPath "Dependencies\ICSharpCode.SharpZipLib.dll")
$FastZip = New-Object -TypeName ICSharpCode.SharpZipLib.Zip.FastZip
$FastZip.CreateZip($LibraryReleaseZip, $LibraryReleaseFolder, $true, "") # zipfile, source, recurse, filter

# Tell the user to commit the version change and tag the revision
""
""
"If you're happy with this release build, you should now run the following commands:"
"    hg com -m `"Set version to $ReleaseVersionNumber`" SolutionInfo.cs"
"    hg tag -m `"Tagged version $ReleaseVersionNumber`" v$ReleaseVersionNumber"